package com.koron.css2;


import cn.hutool.core.util.ObjectUtil;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.ResponseBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.DBSourceUtils;
import com.koron.util.Tools;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;

@Slf4j
@Controller
public class ExportDocAction {
    @Resource
    private Map<String,ExportDocInterface> serviceMap;





    @CrossOrigin
    @RequestMapping(value = "docInterface.api", method = RequestMethod.GET)
    public void service(@RequestParam(required = true, name = "json") String json,HttpServletResponse response, HttpServletRequest request) {
        try {
            log.info("json -----> " + json);
        RequestBean requestBean = com.koron.css2.serviceManage.utils.JsonUtils.jsonToPojo(json, RequestBean.class);
        PrintWriter writer = null;


        // 数据有效性校验
        if (requestBean == null || StringUtils.isBlank(requestBean.getBusicode()) || StringUtils.isBlank(requestBean.getSysType())) {
            log.error("请求的json格式不正确,或者参数为空:"+requestBean.getData().toString());
        }
        // 获取用户信息(包括用户信息，用户可选水司信息，用户当前水司信息)
        UserInfoBean userInfo = Tools.getLoginBean(requestBean.getToken());
        if (userInfo == null) {
            log.error("当前登录信息失效，请重新登录后再操作。");
            throw new RuntimeException("当前登录信息失效，请重新登录后再操作。");
        }
        ResponseBean responseBean = new ResponseBean();
        // 执行服务

            String dbEnv = "_default";
            if(userInfo.getCurService().equals("002")) {
                dbEnv = DBSourceUtils.getDbEnv(userInfo.getCurWaterCode());
            }else if(userInfo.getCurService().equals("003")) {
                //TODO
            }
            if(dbEnv == null){
                throw new RuntimeException("水司编号为："+userInfo.getCurWaterCode()+"的数据源注册失败！");
            }
            ExportDocInterface docInterface = serviceMap.get(requestBean.getBusicode());
            if (ObjectUtil.isNull(docInterface)){
                throw new RuntimeException("服务不存在："+requestBean.getBusicode());
            }
            responseBean.setCode(Constant.MESSAGE_INT_SUCCESS);
            responseBean.setDescription("数据导出成功");
             docInterface.export(response,userInfo, requestBean);
        } catch (Exception e) {
            log.error("操作失败", e);
        }






    }






}
